package com.example.day17im;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;


import com.hyphenate.EMCallBack;
import com.hyphenate.EMMessageListener;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.exceptions.HyphenateException;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.hyphenate.EMError.USER_ALREADY_EXIST;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "TAG";
    @BindView(R.id.bt1)
    Button bt1;
    @BindView(R.id.bt2)
    Button bt2;
    @BindView(R.id.bt3)
    Button bt3;
    @BindView(R.id.bt4)
    Button bt4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
    }

    @OnClick({R.id.bt1, R.id.bt2, R.id.bt3, R.id.bt4})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bt1:  //注册
                Toast.makeText(this, "LLLL", Toast.LENGTH_SHORT).show();
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        //注册失败会抛出HyphenateException
                        try {
                            EMClient.getInstance().createAccount("1006", "123456");//同步方法
                            Log.i(TAG, ": ============注册成功");
                        } catch (HyphenateException e) {
                            switch (e.getErrorCode()){
                                case USER_ALREADY_EXIST :
                                    Log.i(TAG, "run: ------>已注册");
                                    break;
                              default:
                                  e.printStackTrace();
                                   Log.i(TAG, "erro"+e.getErrorCode());
                                   break;
                            }
                        }
                    }
                }).start();
                break;
            case R.id.bt2:
                EMClient.getInstance().login("2001","123456",new EMCallBack() {//回调
                    @Override
                    public void onSuccess() {
                        EMClient.getInstance().groupManager().loadAllGroups();
                        EMClient.getInstance().chatManager().loadAllConversations();
                        Log.e("TAG", "登录聊天服务器成功！");
                    }

                    @Override
                    public void onProgress(int progress, String status) {

                    }

                    @Override
                    public void onError(int code, String message) {
                        Log.e("TAG", "登录聊天服务器失败！");
                    }
                });
                break;
            case R.id.bt3:
                EMClient.getInstance().logout(true, new EMCallBack() {

                    @Override
                    public void onSuccess() {
                        // TODO Auto-generated method stub
                        Log.i(TAG, "onSuccess: 推出登陆成功");
                    }

                    @Override
                    public void onProgress(int progress, String status) {
                        // TODO Auto-generated method stub

                    }

                    @Override
                    public void onError(int code, String message) {
                        // TODO Auto-generated method stub

                    }
                });

                break;
            case R.id.bt4:
                //创建一条文本消息，content为消息文字内容，toChatUsername为对方用户或者群聊的id，后文皆是如此
                EMMessage message = EMMessage.createTxtSendMessage("蟆驴居","2111");
//发送消息
                EMClient.getInstance().chatManager().sendMessage(message);
                break;
        }
    }
}
